<?php

namespace catchAdmin\model;

use think\facade\Cache;
use think\Model;

class AliSms extends Model
{

    private $app = null;


    public function __construct()
    {
        $config = [
            // HTTP 请求的超时时间（秒）
            'timeout'  => 5.0,

            // 默认发送配置
            'default'  => [
                // 网关调用策略，默认：顺序调用
                'strategy' => \Overtrue\EasySms\Strategies\OrderStrategy::class,

                // 默认可用的发送网关
                'gateways' => [
                    'yunpian', 'aliyun',
                ],
            ],
            // 可用的网关配置
            'gateways' => [
                'errorlog' => [
                    'file' => '/tmp/easy-sms.log',
                ],
                'yunpian'  => [
                    'api_key' => '824f0ff2f71cab52936axxxxxxxxxx',
                ],
                'aliyun'   => [
                    'access_key_id'     => 'LTAI5t9mXFn3xRmBf1paea4Z',
                    'access_key_secret' => 'YIoQKLLLcPvlX2QSkcS6rLqvUGn55Y',
                    'sign_name'         => '湖南电十八供应链',
                ],
                //...
            ],
        ];

        $this->app = new \Overtrue\EasySms\EasySms($config);
    }


    public function sendSmsCode($param)
    {
        $code = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
        Cache::set("code_" . $param['mobile'], $code, 60 * 5);

        return $this->app->send($param['mobile'], [
            'template' => 'SMS_324355543',
            'data'     => [
                'code' => $code,
            ],
        ]);
    }
}
